package com.ywyself.ext.kit;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.render.RenderException;
import com.jfinal.render.RenderManager;

/**
 *
 * @author admin
 * @date 2017年10月17日 上午11:45:19
 */
public class YsHandlerKit {

	/**
	 * 渲染一个视图
	 * 
	 * @param view
	 *            视图路径
	 * @param request
	 * @param response
	 * @param isHandled
	 */
	public static void renderView(String view, HttpServletRequest request, HttpServletResponse response,
			boolean[] isHandled) {
		isHandled[0] = true;
		RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render();
	}

	/**
	 * 渲染一个视图
	 * 
	 * @param view
	 *            视图路径
	 * @param data
	 *            页面注入值
	 * @param request
	 * @param response
	 * @param isHandled
	 */
	public static void renderView(String view, Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response, boolean[] isHandled) {
		isHandled[0] = true;
		if (data != null) {
			for (Entry<String, Object> e : data.entrySet()) {
				request.setAttribute(e.getKey(), e.getValue());
			}
		}
		RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render();
	}

	/**
	 * put一个json流到客户机
	 * 
	 * @param jsonText
	 *            json格式的字符串
	 * @param request
	 * @param response
	 * @param isHandled
	 *            在调用了 ActionHandler的 handle 方法之后，isHandled[0] 才会被置为 true。
	 */
	public static void renderJson(String jsonText, HttpServletRequest request, HttpServletResponse response,
			boolean[] isHandled) {
		isHandled[0] = true;
		RenderManager.me().getRenderFactory().getJsonRender(jsonText).setContext(request, response).render();
	}

	public static void render403(HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		isHandled[0] = true;
		RenderManager.me().getRenderFactory().getErrorRender(403).setContext(request, response).render();
	}

	public static void render404(HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		isHandled[0] = true;
		RenderManager.me().getRenderFactory().getErrorRender(404).setContext(request, response).render();
	}

	public static void render500(HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		isHandled[0] = true;
		RenderManager.me().getRenderFactory().getErrorRender(500).setContext(request, response).render();
	}

	/**
	 * 重定向，不携带参数
	 * 
	 * @param url
	 *            转到的url
	 * @param request
	 * @param response
	 * @param isHandled
	 */
	public static void redirect(String url, HttpServletRequest request, HttpServletResponse response,
			boolean[] isHandled) {
		isHandled[0] = true;

		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			throw new RenderException(e);
		}
	}

}
